package cn.xjy.ddlc;

import cn.xjy.ddlc.config.DBConnectionConfig;
import cn.xjy.ddlc.model.Database;
import cn.xjy.ddlc.output.Displayer;
import cn.xjy.ddlc.output.HTMLDisplayer;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.web.WebView;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * GUI 控制器
 *
 * @author Huyahui
 */
@Slf4j
public class Controller {
    
    DatabaseStructureLoader databaseStructureLoader = new DatabaseStructureLoader();
    
    @FXML
    private TextField leftIp;
    
    @FXML
    private TextField leftPort;
    
    // @FXML
    // private ChoiceBox<String> leftType;
    
    @FXML
    private TextField leftUser;
    
    @FXML
    private TextField leftPassword;
    
    @FXML
    private ChoiceBox<String> leftDatabase;
    
    @FXML
    private TextField rightIp;
    
    @FXML
    private TextField rightPort;
    
    // @FXML
    // private ChoiceBox<String> rightType;
    
    @FXML
    private TextField rightUser;
    
    @FXML
    private TextField rightPassword;
    
    @FXML
    private ChoiceBox<String> rightDatabase;
    
    @FXML
    private WebView webView;
    
    @FXML
    public void doConnectionLeft() {
        DBConnectionConfig conf = new DBConnectionConfig(
                // leftType.getValue(),
                leftIp.getText(), leftPort.getText(),
                leftUser.getText(), leftPassword.getText(),
                null);
        List<String> names = databaseStructureLoader.loadAllDatabaseNames(conf);
        ObservableList<String> observableList = FXCollections.observableArrayList(names.toArray(new String[0]));
        leftDatabase.setItems(observableList);
    }
    
    @FXML
    public void doConnectionRight() {
        DBConnectionConfig conf = new DBConnectionConfig(
                // rightType.getValue(),
                rightIp.getText(), rightPort.getText(),
                rightUser.getText(), rightPassword.getText(),
                null);
        List<String> names = databaseStructureLoader.loadAllDatabaseNames(conf);
        ObservableList<String> observableList = FXCollections.observableArrayList(names.toArray(new String[0]));
        rightDatabase.setItems(observableList);
    }
    
    @FXML
    public void doCompare() {
        
        DatabaseComparator comparator = new DatabaseComparator();
        Displayer displayer = new HTMLDisplayer();
        comparator.setDisplayer(displayer);
        long start = System.currentTimeMillis();
        Database leftDb = databaseStructureLoader.loadDatabase(new DBConnectionConfig(
                // leftType.getValue(), 
                leftIp.getText(), leftPort.getText(),
                leftUser.getText(), leftPassword.getText(),
                leftDatabase.getValue()));
        Database rightDb = databaseStructureLoader.loadDatabase(new DBConnectionConfig(
                // rightType.getValue(),
                rightIp.getText(), rightPort.getText(),
                rightUser.getText(), rightPassword.getText(),
                rightDatabase.getValue()));
        log.info("加载表结构数据成功.... 耗时:{}ms", System.currentTimeMillis() - start);
        // 加载db结构
        start = System.currentTimeMillis();
        comparator.compare(leftDb, rightDb);
        log.info("比对数据库结构成功! 耗时:{}ms", System.currentTimeMillis() - start);
        webView.getEngine().loadContent(displayer.getContent().toString());
        
    }
    
}
